package com.ming.yourdiary.adapter;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ming.yourdiary.R;
import com.ming.yourdiary.entity.DiaryNote;
import com.ming.yourdiary.view.DiaryNoteDetailDialog;

import java.util.List;

public class DiaryCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  private static final String TAG = "DiaryCardAdapter";
  private Activity mActivity;
  private List<DiaryNotesItem> mDiaryNotes;


  private Handler mHandler = new Handler(Looper.getMainLooper());

  public DiaryCardAdapter(Activity activity, List<DiaryNotesItem> diaryNotes) {
    mActivity = activity;
    mDiaryNotes = diaryNotes;
  }

  public void setData(@NonNull List<DiaryNotesItem> data) {
    if (data != null) {
      mDiaryNotes = data;
    }
  }

  @NonNull
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView;
    if (viewType == DiaryNotesItem.ITEM_TYPE_CARD) {
      itemView = View.inflate(parent.getContext(), R.layout.diary_card_item_layout, null);
      return new DiaryCardHolder(itemView);
    } else {
      itemView = View.inflate(parent.getContext(), R.layout.diary_month_title_item_layout, null);
      return new DiaryMonthTitleHolder(itemView);
    }
  }

  @Override
  public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof DiaryCardHolder) {
      DiaryCardHolder diaryCardHolder = (DiaryCardHolder) holder;
      diaryCardHolder.setPosition(position);
      diaryCardHolder.refreshData(position);
    } else if (holder instanceof DiaryMonthTitleHolder) {
      DiaryMonthTitleHolder diaryCardHolder = (DiaryMonthTitleHolder) holder;
      diaryCardHolder.setPosition(position);
      diaryCardHolder.refreshData(position);
    }
  }

  public static class DiaryNotesItem {
    public DiaryNote diaryNote;
    public String monthTitle;

    /*ITEM类型：Title 和 Card*/
    public static final int ITEM_TYPE_CARD = 0;//日记卡片
    public static final int ITEM_TYPE_TITLE = 1;//月份标题
    public int type = ITEM_TYPE_CARD;//默认为卡片
  }

  @Override
  public long getItemId(int position) {
    return (long) mDiaryNotes.get(position).hashCode();
  }

  @Override
  public int getItemCount() {
    int count = 0;
    if (mDiaryNotes != null) {
      count = mDiaryNotes.size();
    }
    return count;
  }

  @Override
  public int getItemViewType(int position) {
    DiaryNotesItem diaryNotesItem = mDiaryNotes.get(position);
    return diaryNotesItem.type;
  }

  public DiaryNotesItem getItem(int position) {
    return mDiaryNotes.get(position);
  }


  public class DiaryCardHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


    private TextView mMonthDayText;
    private TextView mWeakDayText;
    private TextView mTimeText;
    private TextView mMYearDayText;
    private TextView mContentText;
    private ImageView mWeatherIcon;

    private int mPosition;

    public DiaryCardHolder(View itemView) {
      super(itemView);
      mMonthDayText = itemView.findViewById(R.id.diary_card_date_day);
      mWeakDayText = itemView.findViewById(R.id.diary_card_date_weekday);
      mTimeText = itemView.findViewById(R.id.diary_card_date_time);
      mMYearDayText = itemView.findViewById(R.id.diary_card_year_day);
      mContentText = itemView.findViewById(R.id.diary_card_note);
      mWeatherIcon = itemView.findViewById(R.id.diary_card_weather_icon);
      itemView.setOnClickListener(this);
    }

    public void setPosition(int position) {
      mPosition = position;
    }

    public void refreshData(int position) {
      if (mDiaryNotes != null && mDiaryNotes.get(position) != null) {
        DiaryNote diaryNote = mDiaryNotes.get(position).diaryNote;
        if (mMonthDayText != null) mMonthDayText.setText(diaryNote.getMonthDay() + "");
        if (mWeakDayText != null) mWeakDayText.setText(diaryNote.getWeekDay());
        if (mTimeText != null) mTimeText.setText(diaryNote.getTime());
        if (mMYearDayText != null) mMYearDayText.setText(diaryNote.getYearDay());
        if (mContentText != null) mContentText.setText(diaryNote.getTitle());
        //TODO 根据WeatherCode获取对应icon
        if (mWeatherIcon != null) mWeatherIcon.setImageResource(R.drawable.weather_100);
      }
    }

    @Override
    public void onClick(View v) {
      if (mDiaryNotes != null && mDiaryNotes.get(mPosition) != null) {
        DiaryNotesItem item = mDiaryNotes.get(mPosition);
        //Toast.makeText(mActivity, item.diaryNote.toString(), Toast.LENGTH_SHORT).show();
        if (item.diaryNote == null) {
          return;
        }
        DiaryNoteDetailDialog.newInstance(mActivity, item.diaryNote).show();
      }
    }
  }

  public class DiaryMonthTitleHolder extends RecyclerView.ViewHolder {
    private TextView mMonthTitle;
    private int mPosition;

    public DiaryMonthTitleHolder(@NonNull View itemView) {
      super(itemView);
      mMonthTitle = itemView.findViewById(R.id.diary_card_month_title);
    }

    public void setPosition(int position) {
      mPosition = position;
    }

    public void refreshData(int position) {
      if (mDiaryNotes != null && mDiaryNotes.get(position) != null) {
        DiaryNotesItem diaryNotesItem = mDiaryNotes.get(position);
        if (mMonthTitle != null) mMonthTitle.setText(diaryNotesItem.monthTitle);
      }
    }


  }


}
